import json
import os.path
import random
from datetime import time

# 累计值
accDict = {}


def realVal(param, ran):
    result = param + random.uniform(-ran, ran)
    return round(result, 4)


# 累计值 存文件，下次执行断点续传
def accVal(key, dv, ran, start):
    if os.path.exists("data/" + key + ".json"):
        with open("data/" + key + ".json", "r") as outfile:
            data = json.load(outfile)
            lastVal = float(data)
    else:
        lastVal = start
    currentVal = lastVal + dv + random.uniform(-ran, ran)
    json_object = json.dumps(currentVal)
    with open("data/" + key + ".json", "w") as outfile:
        outfile.write(json_object)
        outfile.close()
    return round(currentVal, 4)


def onePumpData(nowTime, startTime, endTime):
    pump01_fre = 0
    pump01_op = realVal(0.23, 0.005)
    pump01_rt = accVal("pump0" + str(startTime.hour / 4 + 1) + "rt", 0, 0, 100)
    pump01_sw = 0
    if startTime <= nowTime < endTime:
        # 频率、出水压力、运行时长、开关机状态
        pump01_fre = realVal(43, 0.4)
        pump01_rt = accVal("pump0" + str(startTime.hour / 4 + 1) + "rt", 0.004166, 0.00001, 100)
        pump01_sw = 1
    pump01 = [pump01_fre, pump01_op, pump01_rt, pump01_sw]
    return pump01


def getPumpModelData(now):
    nowTime = now.time()
    # 0-4点开启1#泵,以此类推
    pump01 = onePumpData(nowTime, time(0, 0), time(4, 0))
    pump02 = onePumpData(nowTime, time(4, 0), time(8, 0))
    pump03 = onePumpData(nowTime, time(8, 0), time(12, 0))
    pump04 = onePumpData(nowTime, time(12, 0), time(16, 0))
    pump05 = onePumpData(nowTime, time(16, 0), time(20, 0))
    pump06 = onePumpData(nowTime, time(20, 0), time(23, 59, 59))
    pumpData = [pump01, pump02, pump03, pump04, pump05, pump06]
    dataValues = {}
    for i in range(0, len(pumpData)):
        # 频率
        dataValues["pump0" + str(i + 1) + "_fre"] = pumpData[i][0]
        # 出水压力
        dataValues["pump0" + str(i + 1) + "_op"] = pumpData[i][1]
        # 运行时长
        dataValues["pump0" + str(i + 1) + "_rt"] = pumpData[i][2]
        # 开关机状态
        dataValues["pump0" + str(i + 1) + "_sw"] = pumpData[i][3]
    # 总电耗
    dataValues["to_ele"] = accVal("to_ele", 0.728, 0.020, 5)
    # 总泵房出水瞬时流量
    dataValues["to_flow"] = realVal(2000, 20)
    # 总泵房出水累计流量
    dataValues["acc_flow"] = accVal("acc_flow", 8.3333333, 0.03, 5000)
    # 出水压力
    dataValues["to_press"] = realVal(0.24, 0.002)
    # 吸水井液位
    dataValues["suc_le"] = realVal(3, 0.5)
    return dataValues


def getWaterModelData():
    dataValues = {
        # 清水池液位
        "cl_level": realVal(3, 0.04),
        # 实时进水流量
        "in_flow": realVal(2030, 20),
        # 进水压力
        "in_press": realVal(0.25, 0.002),
        # 累计进水量
        "acc_in": accVal("acc_in", 8.4583333, 0.03, 6000),
        # 实时出水流量
        "out_flow": realVal(2000, 20),
        # 出水压力
        "out_press": realVal(0.24, 0.002),
        # 累计供水量
        "acc_out": accVal("acc_out", 8.3333333, 0.03, 5000),
        # 清水池进水流量
        "cl_in": realVal(2010, 12),
        # 清水池出水流量
        "cl_out": realVal(2008, 12),
        # 接触池液位
        "con_level": realVal(2.95, 0.5),
        # 接触池出水流量
        "con_out": realVal(2006, 12),
        # 滤池液位
        "fil_level": realVal(2.85, 0.5),
        # 滤池出水流量
        "fil_out": realVal(2005, 12),
        # 沉淀池液位
        "sed_level": realVal(2.8, 0.5),
        # 沉淀池进水流量
        "sed_in": realVal(2003, 12)
    }
    return dataValues


def getAddAlumModelData():
    dataValues = {
        # 沉淀池1/3出浊度
        "tru1_3": realVal(0.7, 0.02),
        # 实际PAC计量泵流量
        "coa_flow": realVal(16, 0.05),
        # 累计加矾流量
        "acc_coa": accVal("acc_coa", 0.06667, 0.0002, 50),
        # 原水溶解氧
        "sol_oxy": realVal(10.018, 2),
        # 出水浊度
        "out_tur": realVal(0.03, 0.004),
        # 原水氨氮
        "rw_nitr": realVal(0.1979, 0.003),
        # 原水COD
        "rw_cod": realVal(2.46, 0.3),
        # 原水电导率
        "rw_condu": realVal(150, 30),
        # 原水流量
        "rw_flow": realVal(2030, 20),
        # 原水PH
        "rw_ph": realVal(7.5292, 0.5),
        # 原水温度
        "rw_temper": realVal(20.96, 4),
        # 原水浊度
        "rw_tur": realVal(15, 5),
        # 滤前浊度
        "fiter_tur": realVal(0.92, 0.1)
    }
    return dataValues


def getOnePointData(ap):
    # 臭氧接触池进水流量
    _in = realVal(507.5, 10)
    # 臭氧接触池液位
    _le = realVal(2.95, 0.5)
    # 沉后水（臭氧接触池前）pH
    _ph = realVal(7.5292, 0.5)
    # 沉后水（臭氧接触池前）温度
    _tem = realVal(20.96, 4)
    # 臭氧接触池段前COD
    _icod = realVal(2.46, 0.3)
    # 臭氧投加流量
    _o3 = realVal(16, 0.003)
    # 臭氧投加累计量
    _ao3 = accVal(ap + "ao3", 0.06667, 0.0002, 50)
    # 臭氧接触池段后COD
    _ocod = realVal(0.26, 0.12)
    # 水中余臭氧浓度
    _ro = realVal(0.2, 0.05)
    # 尾气浓度
    _eo = realVal(0.4, 0.08)
    # 臭氧接触池出水累计量
    _caout = accVal(ap + "caout", 2.091, 0.03, 5000)
    # 臭氧接触池累计电耗
    _aele = accVal(ap + "aele", 0.112, 0.020, 5)
    # 出水累计流量
    _aout = accVal(ap + "aout", 2.09, 0.03, 5000)
    point = [_in, _le, _ph, _tem, _icod, _o3, _ao3, _ocod, _ro, _eo, _caout, _aele, _aout]
    return point


def getOzoneModelData():
    point01 = getOnePointData("ap01_")
    point02 = getOnePointData("ap02_")
    point03 = getOnePointData("ap03_")
    point04 = getOnePointData("ap04_")
    pointData = [point01, point02, point03, point04]
    dataValues = {}
    for i in range(0, len(pointData)):
        # 臭氧接触池进水流量
        dataValues["ap0" + str(i + 1) + "_in"] = pointData[i][0]
        # 臭氧接触池液位
        dataValues["ap0" + str(i + 1) + "_le"] = pointData[i][1]
        # 沉后水（臭氧接触池前）pH
        dataValues["ap0" + str(i + 1) + "_ph"] = pointData[i][2]
        # 沉后水（臭氧接触池前）温度
        dataValues["ap0" + str(i + 1) + "_tem"] = pointData[i][3]
        # 臭氧接触池段前COD
        dataValues["ap0" + str(i + 1) + "_icod"] = pointData[i][4]
        # 臭氧投加流量
        dataValues["ap0" + str(i + 1) + "_o3"] = pointData[i][5]
        # 臭氧投加累计量
        dataValues["ap0" + str(i + 1) + "_ao3"] = pointData[i][6]
        # 臭氧接触池段后COD
        dataValues["ap0" + str(i + 1) + "_ocod"] = pointData[i][7]
        # 水中余臭氧浓度
        dataValues["ap0" + str(i + 1) + "_ro"] = pointData[i][8]
        # 尾气浓度
        dataValues["ap0" + str(i + 1) + "_eo"] = pointData[i][9]
        # 臭氧接触池出水累计量
        dataValues["ap0" + str(i + 1) + "_caout"] = pointData[i][10]
        # 臭氧接触池累计电耗
        dataValues["ap0" + str(i + 1) + "_aele"] = pointData[i][11]
        # 出水累计流量
        dataValues["ap0" + str(i + 1) + "_aout"] = pointData[i][12]
    return dataValues
